<?php

// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\controller\Api;
use app\mall\model\Category as CategoryModel;
use app\mall\model\Merch as MerchModel;
use app\mall\model\Qrcode as QrcodeModel;
use app\user\model\User as UserModel;
use qrcode\Qrcode as QrcodeTool;
use think\Db;
/**
 * 二维码控制器
 * @package app\api\controller
 */
class Qrcode extends Api
{
	//生成二维码
    public function index($code = '')
    {
		$url = real_url('api/qrcode/qrcode');
		$url = $url.'?code='.$code;

		$qrcode = new QrcodeTool;
		return $qrcode->make($url, 4);
    }
	
	//二维码跳转
	public function qrcode($code)
	{	
		$url = real_url('user/wechat/wxsign');
		cache('wx'.$code, config('wechat.appid'), 300);
		$url = urlencode($url);
		if (config('wechat.getcode') != '') {
			$api = config('wechat.getcode').$url;
		} else {
			$api = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wechat.appid').'&redirect_uri='.$url.'&response_type=code&scope=snsapi_base&state='.$code.'#wechat_redirect';
		}
		
		header("Location: $api");
	}
	
	//获取登录
	public function code($code = 0)
	{	
		if ($code === 0) $this->error("参数错误");
		if ($this->request->isPost()) {
			$openid = cache('wx'.$code);
			if ($openid == config('wechat.appid') || !$openid) $this->error(false);
			$UserModel = new UserModel();
			$user = $UserModel::where('openid', $openid)->where('status', 1)->find();
			if(!$user){
				$return = [
					'code'	=> 1,
					'msg'	=> '暂无登录权限',
				];
				return $return;
			}
			if($UserModel->autoLogin($user,true)){
				return $this->success('登录成功');
			}
			$this->error(false);
		}
	}
	
	//用户注册
	public function reg(){
		$get_token = session('user_token');
		$code = session('code');
		if(!$get_token || !$code) $this->error("参数错误");
		$api = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$get_token['access_token'].'&openid='.$get_token['openid'].'&lang=zh_CN';
		$userinfo = https_request($api,'GET','json', '');
		if ($this->request->isPost()) {
			$info = $this->request->post();
			// 验证数据
			$result = $this->validate($info, 'Reg');
			if(true !== $result){
			    // 验证失败 输出错误信息
			    $this->error($result);
			}
			//注册用户
			$UserModel = new UserModel();
			$data['role'] = 3;
			$data['openid'] = $get_token['openid'];
			$data['refresh_token'] = $get_token['refresh_token'];
			$data['avatar_img'] = $userinfo['headimgurl'];
			$data['city'] = $userinfo['city'];
			$data['username'] = $userinfo['nickname'];
			$data['nickname'] = $userinfo['nickname'];
			$data['status'] = 1;
			$data['password'] = $info['password'];
			$data['mobile'] = $info['mobile'];
			$data['last_login_time'] = time();
			$data['last_login_ip']  = get_client_ip(1);
			$user = $UserModel::create($data);
			$sign = $UserModel->autoSignin($user->id,true);
			if($sign){
				cache('wx'.$code, $get_token['openid'], 300);
				$this->success('注册成功','index/index/index');
			}
			return $this->error('登录失败');
		}
		return  $this->fetch();
	}
	
	//用户更新
	public function upinfo(){
		$get_token = session('user_token');
		$user = session('user');
		if(!$get_token || !$user) $this->error("参数错误");
		
		//更新用户
		$UserModel = new UserModel();
		//7天不登录，更新头像等数据
		$time = time() - 60*60*24*7;
		if($user['last_login_time'] < $time){
			$api = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$get_token['access_token'].'&openid='.$get_token['openid'].'&lang=zh_CN';
			$userinfo = https_request($api,'GET','json', '');
			$data['avatar_img'] = $userinfo['headimgurl'];
			$data['city'] = $userinfo['city'];
			$data['nickname'] = $userinfo['nickname'];
		}
		$data['id'] = $user['id'];
		$data['refresh_token'] = $get_token['refresh_token'];
		$data['last_login_time'] = time();
		$data['last_login_ip']  = get_client_ip(1);
		$user = $UserModel::update($data);
		$sign = $UserModel->autoSignin($user->id,true);
		if($sign){
			$this->success('登录成功','index/index/index', 1);
		}
		return $this->error('登录失败','index/index/index', 1);
	}

    public function getimg($id = 0)
    {

        if ($id === 0) $this->error('参数错误');
        $qrcode = new QrcodeTool;
        $url = real_url('api/qrcode/index', ['id' => $id]);

        return  $qrcode->make($url);

    }

}
